package com.armail.client.view.javafx;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.util.Callback;

import com.armail.client.dto.Mail;
import com.armail.client.util.DateUtility;
import com.armail.client.view.InboxView;
import com.armail.client.view.InboxViewListener;
import com.armail.client.view.ViewLocation;
import com.armail.client.view.ViewManager;
import com.google.inject.Inject;

public class FXInboxView extends VBox implements InboxView {

	protected final Collection<InboxViewListener> listeners = new LinkedHashSet<InboxViewListener>();
	private TableView<Mail> mailTable;
	private final ViewManager viewManager;
	private final Label mailsLabel = new Label("Inbox");

	@Inject
	public FXInboxView(ViewManager viewManager) {
		this.viewManager = viewManager;
		super.setPrefSize(600, 600);
		super.setSpacing(10);
		super.setPadding(new Insets(10));
		super.getChildren().addAll(mailsLabel, getMailTable());
	}

	public void setModel(List<Mail> mails) {
		getMailTable().setItems(FXCollections.observableArrayList(mails));
	}

	@Override
	public void attachChild(Object subView, ViewLocation location) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void detachChild(Object subView) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void attach() {
		viewManager.attach(this, ViewLocation.INBOX);
	}

	@SuppressWarnings("unchecked")
	private TableView<Mail> getMailTable() {
		if (mailTable == null) {
			final TableView<Mail> table = new TableView<>();
			TableColumn<Mail, String> fromColumn = new TableColumn<>("From");
			fromColumn.setPrefWidth(100);
			fromColumn
					.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Mail, String>, ObservableValue<String>>() {

						@Override
						public ObservableValue<String> call(
								CellDataFeatures<Mail, String> cellData) {
							String name = cellData.getValue().getFromUser()
									.getFirstName()
									+ " "
									+ cellData.getValue().getFromUser()
											.getLastName();
							return new SimpleStringProperty(name);
						}
					});

			TableColumn<Mail, String> subjectColumn = new TableColumn<>(
					"Subject");
			subjectColumn.setPrefWidth(400);
			subjectColumn
					.setCellValueFactory(new PropertyValueFactory<Mail, String>(
							"subject"));

			TableColumn<Mail, Boolean> hasAttachmentColumn = new TableColumn<>(
					"Attachment");
			hasAttachmentColumn.setPrefWidth(100);
			hasAttachmentColumn
					.setCellValueFactory(new PropertyValueFactory<Mail, Boolean>(
							"hasAttachment"));

			TableColumn<Mail, String> recievedDateColumn = new TableColumn<>(
					"Date");
			recievedDateColumn.setPrefWidth(100);
			recievedDateColumn
					.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Mail, String>, ObservableValue<String>>() {
						@Override
						public ObservableValue<String> call(
								CellDataFeatures<Mail, String> cellData) {
							return new SimpleStringProperty(DateUtility
									.convertDate(cellData.getValue()
											.getRecievedDate()));
						}
					});

			table.getColumns().addAll(fromColumn, subjectColumn,
					recievedDateColumn, hasAttachmentColumn);
			mailTable = table;
		}
		return mailTable;
	}

	@Override
	public void detach() {
		viewManager.unregisterLocations(this);
		viewManager.detach(this, ViewLocation.INBOX);
	}

	@Override
	public void done() {
		// TODO Auto-generated method stub

	}

	@Override
	public void addListener(InboxViewListener listener) {
		this.listeners.add(listener);
	}

	@Override
	public void removeListener(InboxViewListener listener) {
		this.listeners.add(listener);
	}

}